$(document).ready(function() {
	
	$("#newProfitID").val($("profitID").val());
	
	$("#pageSelection").bind("change",function(){
		$("#pageIndexField").val($("#pageSelection").val());
		$("#lastCountDate").val($("#countDate").val());
		$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
	});
	
	$("#previousButton").bind("click",function(){
		$("#pageIndexField").val($("#previousPageIndex").val());
		$("#lastCountDate").val($("#countDate").val());
		$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
	});
	
	$("#nextButton").bind("click",function(){
		$("#pageIndexField").val($("#nextPageIndex").val());
		$("#lastCountDate").val($("#countDate").val());
		$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
	});
	
	$("#allChecked").click(function(){
		if($("#allChecked").attr("checked")){
			$("input[name='selectMembers']").attr("checked", true);
		}else{
			$("input[name='selectMembers']").attr("checked", false);
		}
	});
	
	$("input[name='selectMembers']").click(function(){
		if($(this).attr("checked")==undefined ){
			$("#allChecked").attr("checked", false);
		}
	});
	
	$("#batchEditReturnAmount").click(function(){
		var array = $("input[name='selectMembers']:checked");
		var len = array.length;
		if(len==0){
			alert("至少选择一条记录");
			return;
		}
		var memberIdArray="";
		for(i=0;i<len;i++){
			memberIdArray+=$(array[i]).val();
			if((i+1)!=len){
				memberIdArray+=",";
			}
		}
		$("#memberIds").val(memberIdArray);
		$("#editAmountDialog").dialog("open");
	});
});

function editNewAmount(){
	if($("#newAmount").val()==""){
		alert("请输入金额");
	}else{
		$.getJSON(getRootPath()+ "/MainMenu/profit/updateReturnProfitAmount.htm",$("#editNewAmountForm").serializeArray(),function(data){
			if(data.error==undefined){
				$("#editAmountDialog").dialog("close");
				alert("修改成功");
				$("#lastCountDate").val($("#countDate").val());
				$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
			}else{
				alert("修改失败："+data.error);
			}
		});
	}
}

function batchReturnProfit(){
	var array = $("input[name='selectMembers']:checked");
	var len = array.length;
	if(len==0){
		alert("至少选择一条记录");
		return;
	}
	var memberIdArray="";
	for(i=0;i<len;i++){
		memberIdArray+=$(array[i]).val();
		if((i+1)!=len){
			memberIdArray+=",";
		}
	}
	$.getJSON(getRootPath()+ "/MainMenu/profit/batchReturnProfit.htm",{countDate:$("#countDate").val(),memberIDs:memberIdArray,profitID:$("#profitID").val()},function(data){
		if(data.error==undefined){
			alert("返利成功");
			$("#lastCountDate").val($("#countDate").val());
			$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
		}else{
			alert("返利失败："+data.error);
		}
	});
}

function genegateChart(){
	$("#excelForm").submit();
}

function generateExcel(){
	$("#excelTableForm").submit();
}

function returnAllMemberProfit(){
	if(window.confirm("确定要给"+$("#countDate").val()+"当天未返利的会员返还吗?")){
		var url = getRootPath()+ "/MainMenu/profit/returnAllMemberProfit.htm";
		var param = {countDate:$("#countDate").val(),profitID:$("#profitID").val()};
		$.getJSON(url ,param,function(data){
			if(data.error==undefined){
				alert("返利成功");
				$("#lastCountDate").val($("#countDate").val());
				$("#profitTable").load(getRootPath()+ "/MainMenu/profit/pagingList.htm",$("#pagingForm").serializeArray());
			}else{
				alert("返利失败："+data.error);
			}
		});
	}
}

function viewReturnHistory(memberID){
	$("#returnHistoryDiv").dialog({
		modal:true,
		width:350,
		height:400,
		autoOpen:false
	});
	
	$("#returnHistoryDiv").load(getRootPath()+ "/MainMenu/profit/returnHistory.htm",{"memberID":memberID},function(data){
		$("#returnHistoryDiv").dialog("open");
	});
}

